package com.base.cn.platform.os.controller.agent.config;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.agent.config.AgentConfig;
import com.base.cn.platform.os.service.agent.config.AgentConfigBiz;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 分销配置Controller
 *
 * @author LIZHIPENG
 * @since 2018/5/4
 */
@RestController
@RequestMapping("/course")
public class AgentConfigController extends BaseController {

    @Autowired
    private AgentConfigBiz agentConfigBiz;

    /**
     * 获取分销系统配置
     * @return
     */
    @RequestMapping("/queryAgentConfigList")
    public Map<String, AgentConfig> queryAgentConfigList(){
        return agentConfigBiz.queryAgentConfigList();
    }

    /**
     * 保存分销配置
     * @param _params
     * @return
     */
    @RequestMapping("/saveAgentConfig")
    public Map<String, Object> saveAgentConfig(@RequestParam("_params") String _params){
        List<AgentConfig> list = new ArrayList<>();
        Map<String, String> map = gson.fromJson(_params, new TypeToken<Map<String, String>>(){}.getType());
        if(StringUtils.isEmpty(map.get("oneScale"))){
            return ResultUtil.ERROR("请填写一级分成比例输");
        }
        if(StringUtils.isEmpty(map.get("twoScale"))){
            return ResultUtil.ERROR("请填写二级分成比例输入有误");
        }
        if(StringUtils.isEmpty(map.get("bonusScale"))){
            return ResultUtil.ERROR("奖励金比例输入有误，请重新输入");
        }
        Integer oneScale = Integer.valueOf(map.get("oneScale"));
        Integer twoScale = Integer.valueOf(map.get("twoScale"));
        Integer bonusScale = Integer.valueOf(map.get("bonusScale"));
        if(ObjectUtils.isEmpty(oneScale) ||oneScale <0 ||oneScale >100){
            return ResultUtil.ERROR("一级分成比例输入有误，请重新输入");
        }
        if(ObjectUtils.isEmpty(twoScale) || twoScale<0 || twoScale >100){
            return ResultUtil.ERROR("二级分成比例输入有误，请重新输入");
        }
        if(ObjectUtils.isEmpty(bonusScale) || bonusScale <0 ||bonusScale >100){
            return ResultUtil.ERROR("奖励金比例输入有误，请重新输入");
        }
        if(twoScale + bonusScale > 100){
            return ResultUtil.ERROR("二级分成比例与奖励金比例之和不能大于100");
        }
        if(StringUtils.isEmpty(map.get("state"))){
            return ResultUtil.ERROR("推广规则不能为空");
        }
        //申请成为推广员
        // 开启-任意普通学员皆可以申请成为推广员，但需要审核
        // 关闭-不可申请，只允许后台创建
        String agentUserId = map.get("agentUserId");
        String agentUserKey = map.get("agentUserKey");
        String agentUser = map.get("agentUser");
        if(!StringUtils.isNumber(agentUserId)){
            return ResultUtil.ERROR("申请成为推广员配置ID错误，请检查");
        }
        if(StringUtils.isEmpty(agentUser) || (!agentUser.equals("ON") && !agentUser.equals("OFF"))){
            return ResultUtil.ERROR("请勾选是否允许申请成为推广员开关");
        }
        if(StringUtils.isEmpty(agentUserKey) || !agentUserKey.equals("AGENTUSER")){
            return ResultUtil.ERROR("申请成为推广员配置类型错误，请检查");
        }
        AgentConfig agentConfig = this.getAgentConfig(agentUserId,agentUserKey,agentUser,null);
        list.add(agentConfig);

        //推广员审核
        // 开启-开启后，学员申请成为推广员需要审核
        // 关闭-学员直接申请成功，不需要审核
        String userAuditingId = map.get("userAuditingId");
        String userAuditingKey = map.get("userAuditingKey");
        String userAuditing = map.get("userAuditing");
        if(!StringUtils.isNumber(userAuditingId)){
            return ResultUtil.ERROR("推广员审核配置ID错误，请检查");
        }
        if(StringUtils.isEmpty(userAuditing) || (!userAuditing.equals("ON") && !userAuditing.equals("OFF"))){
            return ResultUtil.ERROR("请勾选推广员审核开关");
        }
        if(StringUtils.isEmpty(userAuditingKey) || !userAuditingKey.equals("USERAUDITING")){
            return ResultUtil.ERROR("推广员审核配置类型错误，请检查");
        }
        agentConfig = this.getAgentConfig(userAuditingId,userAuditingKey,userAuditing,null);
        list.add(agentConfig);

        //是否允许推广员邀请好友成为推广员
        // 开启-可以邀请好友成为推广员
        // 关闭-无法邀请好友成为推广员，且不具备邀请功能
        String userInviteId = map.get("userInviteId");
        String userInviteKey = map.get("userInviteKey");
        String userInvite = map.get("userInvite");
        if(!StringUtils.isNumber(userInviteId)){
            return ResultUtil.ERROR("是否允许邀请配置ID错误，请检查");
        }
        if(StringUtils.isEmpty(userInvite) || (!userInvite.equals("ON") && !userInvite.equals("OFF"))){
            return ResultUtil.ERROR("请勾选是否允许邀请好友开关");
        }
        if(StringUtils.isEmpty(userInviteKey) || !userInviteKey.equals("USERINVITE")){
            return ResultUtil.ERROR("是否允许邀请配置类型错误，请检查");
        }
        agentConfig = this.getAgentConfig(userInviteId,userInviteKey,userInvite,null);
        list.add(agentConfig);

        //是否开启推广中心入口
        // 开启-打开前台推广中心入口
        // 关闭-关闭前台推广中心入口
        String agentCenterId = map.get("agentCenterId");
        String agentCenterKey = map.get("agentCenterKey");
        String agentCenter = map.get("agentCenter");
        if(!StringUtils.isNumber(agentCenterId)){
            return ResultUtil.ERROR("开启推广中心入口ID错误，请检查");
        }
        if(StringUtils.isEmpty(agentCenter) || (!agentCenter.equals("ON") && !agentCenter.equals("OFF"))){
            return ResultUtil.ERROR("请勾选开启推广中心入口开关");
        }
        if(StringUtils.isEmpty(agentCenterKey) || !agentCenterKey.equals("AGENTCENTER")){
            return ResultUtil.ERROR("开启推广中心入口类型错误，请检查");
        }
        agentConfig = this.getAgentConfig(agentCenterId,agentCenterKey,agentCenter,null);
        list.add(agentConfig);
        //设置推广员的分成比例
        AgentConfig agentConfigIncome = new AgentConfig();
        if(StringUtils.isNotEmpty(map.get("incomeId"))){
            agentConfigIncome.setId(new BigDecimal(map.get("incomeId")));
        }
        agentConfigIncome.setConfigType(ConfigName.SALES_INCOME);
        Map<String,String> agentIncomeMap = new HashMap<>();
        agentIncomeMap.put("oneScale",map.get("oneScale"));
        agentIncomeMap.put("twoScale",map.get("twoScale"));
        agentIncomeMap.put("bonusScale",map.get("bonusScale"));
        agentIncomeMap.put("state",map.get("state"));
        String context = gson.toJson(agentIncomeMap);
        agentConfigIncome.setContext(context);
        list.add(agentConfigIncome);
        //设置更新时间
        for(AgentConfig agentConfigs:list){
            agentConfigs.setUpdateTime(new Date());
        }
        return agentConfigBiz.saveAgentConfig(list);
    }

    //=============================================================
    private AgentConfig getAgentConfig(String id, String configKey, String switchKey, Integer num){
        BigDecimal _id = new BigDecimal(id);
        AgentConfig agentConfig = new AgentConfig();
        agentConfig.setId(_id);
        agentConfig.setConfigType(configKey);
        Map<String,String> map = new HashMap<>();
        map.put("switch",switchKey);
        if(num!=null){
            map.put("num",String.valueOf(num));
        }
        String context = gson.toJson(map);
        agentConfig.setContext(context);
        return agentConfig;
    }
}
